package com.sctu.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.sctu.EMSApp.*;
import com.sctu.util.SharePreferenceUtil;


/**
 * Created by zhu on 2015/11/3.
 */
public class MyinfoFragment extends Fragment implements View.OnClickListener {
    private View view;
    private TextView name;
    private LinearLayout myOption;
    private String islogin;
    private ImageView mannerge, tourlist, editMsg, logout,headImg;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_my, container, false);
        setView();
        setClick();
        util();
        return view;


    }

    @Override
    public void onResume() {
        super.onResume();
        String myName = SharePreferenceUtil.getInstance(getActivity()).getKeyData("username");
        if (myName.equals("")) {
            headImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), LoginActivity.class);
                    getActivity().startActivity(intent);
                }
            });
            name.setText("未登录");
        } else {
            name.setText(myName);
            if (myOption.getChildAt(2) == null) {
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lp.setMargins(10, 10, 10, 10);
                editMsg.setImageResource(R.drawable.my_edit_msg);
                editMsg.setLayoutParams(lp);
                myOption.addView(editMsg, 2);
            }
        }

    }

    private void util() {

    }

    private void setClick() {
        mannerge.setOnClickListener(this);
        tourlist.setOnClickListener(this);
        editMsg.setOnClickListener(this);
        logout.setOnClickListener(this);
    }

    private void setView() {
        mannerge = (ImageView) view.findViewById(R.id.my_manager_tour_id);
        tourlist = (ImageView) view.findViewById(R.id.my_tour_list_id);
        name = (TextView) view.findViewById(R.id.my_username);
        myOption = (LinearLayout) view.findViewById(R.id.my_option);
        editMsg = new ImageView(getActivity());
        logout = (ImageView) view.findViewById(R.id.my_logout);
        headImg = (ImageView) view.findViewById(R.id.my_tour_list_head_img_id);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        islogin = SharePreferenceUtil.getInstance(getActivity()).getKeyData("islogin");
        if (v == editMsg) {
            intent.setClass(getActivity(), EditActivity.class);
            getActivity().startActivity(intent);
        }
        switch (v.getId()) {
            case R.id.my_manager_tour_id:
                if (islogin.equals("1")) {
                    Intent intent1 = new Intent();
                    intent1.setClass(getActivity(), MyinfoManageNotesActivity.class);
                    startActivity(intent1);
                } else {
                    intent.setClass(getActivity(), LoginActivity.class);
                    getActivity().startActivity(intent);
                }

                break;

            case R.id.my_tour_list_id:

                TravelNotesListActivity.actionStart(getActivity());

                break;
            case R.id.my_logout:
                SharePreferenceUtil.getInstance(getActivity()).setKeyData("islogin", "");
                SharePreferenceUtil.getInstance(getActivity()).setKeyData("username", "");
                SharePreferenceUtil.getInstance(getActivity()).setKeyData("userId", "");
                intent.setClass(getActivity(), LoginActivity.class);
                getActivity().startActivity(intent);
                break;
        }
    }
}
